package gui.common;



import gui.ButtonName;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import java.awt.LayoutManager;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import data.Forum;
import data.Group;

public class ForumListPanel extends JPanel{
	
	
	private static final long serialVersionUID = 1L;
	
	//top
	private JTextField txtGroupName;
	private JTextField txtGroupCreater;
	private JTextField txtGroupDate;
	
	private JTextArea groupDescriptions;
	//center
	private JList forumList;
	protected JScrollPane scroll;
	//right
	private JButton btnStarChat;
	private JButton btnViewMessage;
	private JButton btnLeaveGroup;
	private JButton btnViewForum;
	private JButton btnNewForum;
	private JButton btnDeleteForum;
	
	private JPanel topPanel;
	private JPanel rightPanel;
	private JPanel centerPanel;
	
	private LayoutManager topLayout=null;
	private LayoutManager rightLayout=null;
	private LayoutManager centerLayout=null;
	
	private ActionListener btnListener=null;
	private List<Forum> forumData=null;
	private Group group=null;
	
	private Forum currentForum=null;
	
	public Forum getCurrentForum() {
		return currentForum;
	}

	public ForumListPanel(Group group,List<Forum> data,ActionListener listener){
		this.forumData=data;
		this.btnListener=listener;
		this.group=group;
		initUI();
		setEventListener();
	}
	protected  void setEventListener(){
		MouseListener mouseListener = new MouseAdapter() {
		     public void mouseClicked(MouseEvent e) {
		         int index = forumList.locationToIndex(e.getPoint());
		         currentForum=forumData.get(index);
		         System.out.println("Double clicked on Item " + currentForum);
		         
		     }
		 };
		 forumList.addMouseListener(mouseListener);
	}
	protected void initUI(){
		this.setLayout(new BorderLayout());
		initTop();
		initRight();
		initCenter();
		this.add(topPanel,BorderLayout.NORTH);
		this.add(centerPanel,BorderLayout.CENTER);
		this.add(rightPanel,BorderLayout.EAST);
		
	}
	
	protected void initTop(){
		topPanel=new JPanel();
		
		txtGroupName=new JTextField(group.getName());
		txtGroupName.setBorder(BorderFactory.createTitledBorder("Group Name"));
		txtGroupName.setEditable(false);
		
		txtGroupCreater=new JTextField(group.getCreaterName());
		txtGroupCreater.setBorder(BorderFactory.createTitledBorder("Creater"));
		txtGroupCreater.setEditable(false);
		
		txtGroupDate=new JTextField(group.getDateString());
		txtGroupDate.setBorder(BorderFactory.createTitledBorder("Date"));
		txtGroupDate.setEditable(false);
		
		groupDescriptions=new JTextArea(group.getDescription());
		groupDescriptions.setBorder(BorderFactory.createTitledBorder("Descriptions"));
		groupDescriptions.setEditable(false);
		
		topPanel.setLayout(new GridLayout(2,1));
		
		JPanel up=new JPanel();
		up.setLayout(new GridLayout(1,3));
		up.add(txtGroupName);
		up.add(txtGroupCreater);
		up.add(txtGroupDate);
		
		topPanel.add(up);
		topPanel.add(groupDescriptions);
		
		 
         
	}
	
	protected void initCenter(){
		centerPanel=new JPanel();
		centerPanel.setBorder(BorderFactory.createTitledBorder("Forum List Info"));
		centerLayout=new GridLayout(1,1);
		//forumData
		Vector<Forum> data=new Vector<Forum>();
		for (Forum forum : forumData) {
			data.add(forum);
		}
		forumList=new JList(data);
		scroll=new JScrollPane();
		scroll.setViewportView(forumList);
		centerPanel.add(scroll);
		
		
	}
	protected void initRight(){
		rightPanel=new JPanel();
		rightPanel.setBorder(BorderFactory.createTitledBorder("Avilable Operation"));
		
		rightLayout=new GridLayout(8,1);
		((GridLayout)rightLayout).setVgap(10);
		
		rightPanel.setLayout(rightLayout);
		
		btnStarChat=new JButton(ButtonName.START_CHAT);
		btnStarChat.addActionListener(btnListener);
		btnViewMessage=new JButton(ButtonName.ENTER_FORUM);
		btnViewMessage.addActionListener(btnListener);
		btnLeaveGroup=new JButton(ButtonName.LEAVE_GROUP);
		btnLeaveGroup.addActionListener(btnListener);
		btnViewForum=new JButton(ButtonName.VIEW_FORUM);
		btnViewForum.addActionListener(btnListener);
		btnNewForum=new JButton(ButtonName.CREATE_NEW_FORUM);
		btnNewForum.addActionListener(btnListener);
		
		btnDeleteForum=new JButton(ButtonName.DELETE_FORUM);
		btnDeleteForum.addActionListener(btnListener);
		btnDeleteForum.setVisible(false);
		
		rightPanel.add(btnStarChat);
		rightPanel.add(btnNewForum);
		rightPanel.add(btnViewMessage);
		rightPanel.add(btnLeaveGroup);
		
	}

	
	public void showAdminButtons(){
		btnDeleteForum.setVisible(true);
	}
	
	public void showUserButttons(){
		
	}
	
	public static void main(String[] args) {
		Forum d=new Forum();
		d.setId(1);
		d.setUserName("ym221479");
		d.setSubject("help");
		d.setMessage("One common use for tModel entities is to represent technical specifications or concepts. For example, a tModel can be used to represent a specification that defines wire protocols,");
		d.setTime(new Date(System.currentTimeMillis()));
		List<Forum> data=new ArrayList<Forum>();
		data.add(d);
		Group g=new Group();
		g.setName("HiFi Group");
		g.setCreaterName("scm");
		g.setCreated_date(new Date(System.currentTimeMillis()-90*24*60*60000));
		g.setDescription("a group for any one like hifi\nant one");
		JPanel ss=new ForumListPanel(g,data,null);
		new gui.server.TestPanel(ss);
	}
}
